/*
 * Copyright (c) 2006, 2023, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

/**
 * <p>Project: jse2302 - Ticket
 * <p>Powered by webrx On 2023-06-30 15:48:57
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Ticket implements Runnable{
    private int num = 100;

    private Object lock = new Object();

    public Ticket() {
    }

    public Ticket(int num) {
        this.num = num;
    }

    @Override
    public void  run() {
        String tn = Thread.currentThread().getName();
        System.out.printf("%s:线程启动%n", tn);
        while(true){
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            synchronized (this) {
                if (num < 1) break;
                System.out.printf("%s:售出一张票(座号:%d)，剩余%d张票%n", tn, num, --num);
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
